package com.ysd.lis.controller.emr;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.emr.EmrItem;
import com.ysd.lis.entity.emr.EmrItemStandard;
import com.ysd.lis.service.emr.EmrItemService;
import com.ysd.lis.service.emr.EmrItemStandardService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wh
 * @since 2025-06-05
 */
@RestController
@RequestMapping("/emr/item")
public class EmrItemController extends BaseController {

    @Autowired
    private EmrItemService emrItemService;
    @Autowired
    EmrItemStandardService emrItemStandardService;

    @ApiOperation("查询")
    @PostMapping("/findEmrItemList")
    public Result findEmrItemList(@RequestBody EmrItem emrItem) {
        return emrItemService.findEmrItemList(emrItem);
    }

    @ApiOperation("保存")
    @PostMapping("/addOrUpdateEmrItem")
    public Result addOrUpdateEmrItem(@RequestBody EmrItem emrItem) {
        boolean save = emrItemService.saveOrUpdate(emrItem);
        if (save) {
            return Result.succ(1, "保存成功", null);
        }
        return Result.fail("保存失败");
    }

    @ApiOperation("删除")
    @PostMapping("/deleteEmrItemList")
    public Result deleteEmrItemList(@RequestBody List<EmrItem> emrItems) {
        List<String> ids = emrItems.stream().map(EmrItem::getId).collect(Collectors.toList());
        for (EmrItem emrItem : emrItems) {
            emrItemStandardService.remove(new LambdaQueryWrapper<EmrItemStandard>().eq(EmrItemStandard::getVersionCode, emrItem.getVersionCode()).eq(EmrItemStandard::getRoleCode, emrItem.getRoleCode()).eq(EmrItemStandard::getItemCode, emrItem.getItemCode()));
        }
        boolean b = emrItemService.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        }
        return Result.fail("删除失败");
    }

}
